﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhuyenMaiVang.Models;
using KhuyenMaiVang.Helpers;
using KhuyenMaiVang.DAL;

namespace KhuyenMaiVang.Controllers
{
    public class CheckOutController : Controller
    {
        private KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();
        //
        // GET: /CheckOut/
        [Authorize(Roles = "Member")]
        public ActionResult Index()
        {
            CartViewModel model = new CartViewModel();
            User user = ProcessHelper.getUser(User.Identity.Name);
            Customer customer = new Customer();
            customer.UserID = user.ID;
            customer.User = user;
            model.MyCustomer = customer;
            List<int> ids = getSession();
            List<OrderDetail> lstOrders = new List<OrderDetail>();
            if (ids != null)
            {
                foreach (int id in ids)
                {
                    Voucher vouch = db.Vouchers.Single(m => m.ID == id);
                    OrderDetail order = new OrderDetail();
                    order.VoucherID = vouch.ID;
                    order.Voucher = vouch;
                    lstOrders.Add(order);
                }
            }
            model.MyOrderDetails = lstOrders;
            return View(model);
        }
        [HttpPost]
        [Authorize(Roles = "Member")]
        public ActionResult Index(CartViewModel model)
        {
            List<int> ids = getSession();
            List<OrderDetail> lstOrderDetail = new List<OrderDetail>();
            foreach (int id in ids)
            {
                Voucher vouch = db.Vouchers.Single(m => m.ID == id);
                OrderDetail item = new OrderDetail();
                item.VoucherID = vouch.ID;
                item.Voucher = vouch;
                lstOrderDetail.Add(item);
            }
            model.MyOrderDetails = lstOrderDetail;
            //Save customer
            Customer cutomer = model.MyCustomer;
            User cur = ProcessHelper.getUser(User.Identity.Name);
            cutomer.UserID = cur.ID;
            db.Customers.AddObject(cutomer);
            db.SaveChanges();
            int idx = db.Customers.ToList().Count;
            Customer cut2 = db.Customers.ToList()[idx-1];
            int id_customer = cut2.ID;
            //Save Order
            decimal? amount = 0;
            foreach (OrderDetail item in model.MyOrderDetails)
            {
                Voucher vouch = item.Voucher;
                amount += vouch.SellPrice;
            }
            Order order = new Order();
            order.Amount = amount;
            order.CreatedTime = DateTime.Now;
            order.Payment = 1;
            order.Status = 1;
            order.CustomerID = id_customer;
            db.Orders.AddObject(order);
            db.SaveChanges();
            int idxx = db.Orders.ToList().Count;
            Order cutx2 = db.Orders.ToList()[idxx-1];
            int id_order = cutx2.ID;
            //Save Order Detail
            for (int i=0; i<lstOrderDetail.Count; i++)
            {
                OrderDetail item = new OrderDetail();
                item.VoucherID = lstOrderDetail[i].VoucherID;
                item.Status = 1;
                item.OrderID = id_order;
                db.OrderDetails.AddObject(item);
                db.SaveChanges();
                item = null; 
            }
            
            return RedirectToAction("Index", "Home");
        }
        /**
         * Helper
         * */
        public List<int> getSession()
        {
            List<int> ids = Session["myVouchers"] != null ? (List<int>)Session["myVouchers"] : null;
            return ids;
        }
    }
}
